package com.idbk.chargestation.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.idbk.chargestation.R;
import com.idbk.chargestation.bean.JsonGunInfor;
import com.idbk.chargestation.bean.JsonPointPile;

import java.util.ArrayList;
import java.util.List;

/**充电枪详情设配器
 * Created by wuz on 2019/1/10.
 */

public class gunDetailAdapter extends BaseAdapter {

    private Context mContext;
    private LayoutInflater mInflater;

    private JsonPointPile mData;
    private List<JsonGunInfor> mInforList;

    public gunDetailAdapter(Context context, JsonPointPile data){
        mContext = context;
        mInflater = LayoutInflater.from(mContext);
        mData = data;

        mInforList = new ArrayList<JsonGunInfor>();
        mInforList.addAll(mData.gunInfos);
        setGunSort(mInforList);
    }

    private void setGunSort(List<JsonGunInfor> list){
        int num1, num2;
        JsonGunInfor sign;
        for(int i = 0; i < list.size()-1; i++){
            for (int j = i+1; j < list.size(); j++){
                num1 = list.get(i).gunNum;
                num2 = list.get(j).gunNum;
                if (num2 < num1){
                    sign = list.get(i);
                    list.set(i, list.get(j));
                    list.set(j, sign);
                }
            }
        }
    }

    private class Holder {
        private ImageView mImageStatus;
        private TextView mTextGunNum;
        private TextView mTextGunRate;
        private TextView mTextGunType;
        private TextView mTextGunMethod;
        private TextView mTextGunPrice;
    }

    @Override
    public int getCount() {
        return mInforList.size();
    }

    @Override
    public Object getItem(int position) {
        return mInforList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v;
        Holder hold;
        if (convertView == null) {
            v = mInflater.inflate(R.layout.item_pile_detail, parent, false);
            hold = new Holder();

            hold.mImageStatus = (ImageView) v.findViewById(R.id.imageViewStatus);

            hold.mTextGunNum = (TextView) v.findViewById(R.id.textview_gun_num);
            hold.mTextGunRate = (TextView) v.findViewById(R.id.textview_gun_rate);
            hold.mTextGunType = (TextView) v.findViewById(R.id.textview_gun_type);
            hold.mTextGunMethod = (TextView) v.findViewById(R.id.textview_gun_method);
            hold.mTextGunPrice = (TextView) v.findViewById(R.id.textview_gun_price);

            v.setTag(hold);
        } else {
            v = convertView;
            hold = (Holder) v.getTag();
        }
        if (mInforList.get(position).gunStatus == 1){
            hold.mImageStatus.setImageResource(R.drawable.ico_map_pile_disabled);
        }else if (mInforList.get(position).gunStatus == 2){
            hold.mImageStatus.setImageResource(R.drawable.ico_map_pile_warning);
        }else if (mInforList.get(position).gunStatus == 3){
            hold.mImageStatus.setImageResource(R.drawable.ico_map_pile_free);
        }else if (mInforList.get(position).gunStatus == 4){
            hold.mImageStatus.setImageResource(R.drawable.ico_map_pile_using);
        }else if (mInforList.get(position).gunStatus == 10){
            hold.mImageStatus.setImageResource(R.drawable.ico_map_pile_offline);
        }else {
            hold.mImageStatus.setImageResource(R.drawable.ico_map_pile_using);
        }

        hold.mTextGunNum.setText("枪" + mInforList.get(position).gunNum);
        String fast = "超速", slow = "慢速";
       if (mData.chargeRate.equals(fast)){
           hold.mTextGunRate.setText("快充");
           hold.mTextGunRate.setBackgroundResource(R.drawable.textview_style_blue);
       }else if (mData.chargeRate.equals(slow)){
           hold.mTextGunRate.setText("慢充");
       }

       hold.mTextGunType.setText(" " + mData.performance);

        hold.mTextGunMethod.getPaint().setFlags(Paint. UNDERLINE_TEXT_FLAG );
        if (mData.priceTypeEnum == 1){
            hold.mTextGunMethod.setText("通用电价");
        }else if (mData.priceTypeEnum == 2){
            hold.mTextGunMethod.setText("分时电价");
        }else {
            hold.mTextGunMethod.setText("N/A");
        }

        hold.mTextGunPrice.setText(mData.getInfor());

        return v;
    }
}
